<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>jquery ajax</title>
    <script src="js/jquery-1.12.4.js"></script>
</head>
<body>
<input id="btn1" type="button" value="get请求">
<input id="btn2" type="button" value="post请求">
</body>
<script>
    $(function() {
        $("#btn1").click(function(){
            // $.get("/test?id=2&username=lisi", function(data){
            //     console.log(data);
            // });
            /**
             * 可以发送任意请求方式: get, post, put , delete ,option, .....
             * $.ajax({
                    type: "get/post/put/delete",
                    url: "/url",
                    data: {请求参数数据},
                    contentType: "请求的数据类型",
                    dataType: "响应的数据类型",
                    //成功的回调函数
                    // data 响应的数据
                    success: function(data){

                    },
                    //失败的回调函数, data 失败的相应数据
                    error:function(data) {

                    }
                });
             */
            $.ajax({
                type: "get",
                url: "/test",
                data: {"id":1, "username": "张三"},
                success:function(data){
                    console.log(data);
                }
            });

        });

        $("#btn2").click(function(){
            let user = {"id":1, "username": "孙浩", "sex": "男", "address":"北京北科"};
            let json = JSON.stringify(user);
            // $.post("/test",json , function(data){
            //     console.log(data);
            // });
            $.ajax({
                type: "post",
                url: "/test",
                data:json,
                contextType: "application/json",
                success: function(data) {
                    console.log(data);
                }
            });
        });
    });
</script>
</html>